package com.moyu.plugin

import com.android.build.api.instrumentation.FramesComputationMode
import com.android.build.api.instrumentation.InstrumentationScope
import com.android.build.api.variant.AndroidComponentsExtension
import com.moyu.plugin.cat.CatClassVisitorFactory
import com.moyu.plugin.extension.PhoenixExtension
import com.moyu.plugin.extension.TransformExtension
import com.moyu.plugin.extension.YechaoabPluginExtension
import org.gradle.api.Plugin
import org.gradle.api.Project

class TestPlugin : Plugin<Project> {
    override fun apply(project: Project) {

        project.extensions.create("yechaoa", YechaoabPluginExtension::class.java)
        project.extensions.create("phoenix", PhoenixExtension::class.java)

        project.afterEvaluate {
            printYechaoa(project)
        }

        val androidComponents = project.extensions.getByType(AndroidComponentsExtension::class.java)
        androidComponents.onVariants { variant ->
            val transformExtension = getTransformConfig(project)
            println("variant=${variant.name}")
            variant.instrumentation.setAsmFramesComputationMode(FramesComputationMode.COPY_FRAMES)
            variant.instrumentation.transformClassesWith(
                CatClassVisitorFactory::class.java,
                InstrumentationScope.ALL
            ) { params ->
                params.catOn.set(transformExtension.catOn)
                params.catName.set(transformExtension.extensionTitle)
            }
        }
    }

    private fun getTransformConfig(project: Project): TransformExtension {
        printPhoenix(project)
        val phoenix = project.extensions.findByType(PhoenixExtension::class.java)
        if (phoenix == null) {
            val transformExtension = TransformExtension()
            transformExtension.catOn = false
            return transformExtension
        }
        return phoenix.transform

    }

    private fun printYechaoa(project: Project) {
        val extension = project.extensions.findByType(YechaoabPluginExtension::class.java)
        if (extension != null) {
            println()
            println("===================================yechaoa=================================")
            println("title = ${extension.title}")
            println("chapter = ${extension.chapter}")
            println("author = ${extension.subExtension?.author}")
            println("===================================yechaoa=================================")
            println()
        }
    }

    private fun printPhoenix(project: Project) {
        val phoenix = project.extensions.findByType(PhoenixExtension::class.java)
        if (phoenix != null) {
            println()
            println("===================================PhoenixPlugin=================================")
            println("name = ${phoenix.name}")
            println("description = ${phoenix.transform.toString()}")
            println("===================================PhoenixPlugin=================================")
            println()
        }

    }
}